<template>
  <dir>
    <el-table
      :data="list"
      border
      style="width: 100%"
    >

      <el-table-column
        prop="name"
        label="姓名"
        width="120"
      >
      </el-table-column>

      <el-table-column
        prop="address"
        label="地址"
        width="300"
      >
      </el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        width="100"
      >
        <template slot-scope="scope">
          <el-button
            @click="handleClick(scope.row)"
            type="text"
            size="small"
          >查看</el-button>
          <el-button
            type="text"
            size="small"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block">

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </dir>
</template>

<script>
import axios from "axios";
export default {
  created() {
    axios.get("/api/list").then((res) => {
      console.log("res", res);
      this.originList = res.data.data;
      this.total = this.originList.length;

      this.refreshList();
      console.log("this.list", this.list);
    });
  },
  methods: {
    refreshList() {
      this.list = this.originList.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      );
    },
    handleClick(row) {
      console.log(row);
      this.$router.push("/admin/detail/" + row.name);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.refreshList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.refreshList();
    },
  },

  data() {
    return {
      currentPage: 1,
      total: 0,
      pageSize: 5,
      list: [],
      originList: [],
    };
  },
};
</script>